import Taro ,{ Component } from '@tarojs/taro'
import { View , Image} from '@tarojs/components'

import './myConvenienceList.scss'

import ConvenienceItem from '../../component/ConvenienceItem/convenienceItem.js'

import postImage from '../../image/post.png'

import DataManager from '../../utils/DataManager.js'
import ListEmptyView from '../../component/LiseEmptyView/listEmptyView.js'
import emptyIcon from '../../image/ic_empty.png'

import {base_url} from "../../network/config";
import BasePage from "../basePage";
import {COMMON_CONST} from "../../utils/const";

let mPageNum = 1;

export default class myConvenienceList extends BasePage {
  config = {
    navigationBarTitleText: '我的便民信息'
  }

  constructor() {
    super(...arguments)
    this.state = {
      list: [],
      bannerHeight:0,
    }
  }

  componentWillMount(){
  }

  componentDidShow(){
  }

  componentDidMount () {
    this.loadConvenienceList(1)

    Taro.eventCenter.on('shopInfoPost',() => {
      mPageNum = 1;
      this.loadConvenienceList(mPageNum)
    })
  }

  onPullDownRefresh(){
    mPageNum = 1;
    this.loadConvenienceList(mPageNum);
  }

  onReachBottom(){
    mPageNum ++;
    this.loadConvenienceList(mPageNum)
  }

  loadConvenienceList(pageNo){
    console.log('loadpage------------' + pageNo)
    let params = {
      token:DataManager.getToken(),
      platform:'android_c',
      weixinId:DataManager.getWeiXinId(),
      loginChannel:'c',
      appId:'KC876D98B30UA40CJSNHI689FB33SDW',
      digest : 'dda484557c12febe48139d8f44051c13',
      pageNo: pageNo,
      pageSize: COMMON_CONST.LIST_SIZE
    }

    let _this = this;
    let url = base_url + 'stateless/category/news/userCategoryNewsList'
    Taro.request({
      url:url,
      data: params,
      method: 'GET',
      success(res) {
        if (!res.data || !res.data.data || !res.data.data.list || res.data.data.list.length < 1){
          if (mPageNum > 1){
            mPageNum --;
          }
        }

        if (!res || !res.data || !res.data.data || !res.data.data.list){
          return
        }

        let shops = [];
        if (pageNo == 1){
          shops = res.data.data.list;
        } else {
          shops = _this.state.list.concat(res.data.data.list)
        }

        _this.setState({
          list: shops
        })
      } ,
      fail: function () {
        if (mPageNum > 1){
          mPageNum --;
        }
      },
      complete:function(){
        Taro.stopPullDownRefresh();
      }
    })
  }

  handlePostClick = () =>{
    Taro.navigateTo({
      url:`/pages/post/allPost`
    })
  }

  render() {
    return (
      <View className='shop-list-main-container'>
        <View className='shop-list-container'>
          {this.state.list.length > 0 ? this.state.list.map(item => {
              return (<ConvenienceItem className='shop-list-item' infoData={item}></ConvenienceItem>)
            })
            :
            <ListEmptyView emptyText='暂无数据' emptyImage={emptyIcon}/>
          }
        </View>
        <View className='post-entry' >
          <Image  src={postImage} style='width:50px; height:50px;position:relative' onClick={this.handlePostClick.bind(this)} />
        </View>
      </View>
    )
  }

}
